from mimetypes import init
import sqlite3
from typing import Dict, Tuple
from numpy import int32

from pyparsing import Char
import torch

class HandianData:

    cursor = None

    @staticmethod
    def init():
        file_name = r'./data/handian/dict.db'
        disk_db = sqlite3.connect(file_name, check_same_thread=False)
        mem_db = sqlite3.connect(':memory:', check_same_thread=False)
        disk_db.backup(mem_db)
        HandianData.cursor = mem_db.cursor()

    strokes_token_cache:Dict[Tuple[str, int], str] = dict()    
    @staticmethod
    def get_strokes_seq_cached(char:str, max_stroke_num:int = 20):
        if HandianData.cursor is None:
            HandianData.init()
        if (char, max_stroke_num) not in HandianData.strokes_token_cache:
            if '\u4e00' <= char <= '\u9fff':
                query = f"select BiShun2 from Dicts where Char = '{char}'"
                HandianData.cursor.execute(query)
                rows = HandianData.cursor.fetchone()
                if rows[0] is not None:
                    seq = rows[0]
                    HandianData.strokes_token_cache[(char, max_stroke_num)] = seq
                else:
                    HandianData.strokes_token_cache[(char, max_stroke_num)] = ''
            else:
                HandianData.strokes_token_cache[(char, max_stroke_num)] = ''
        return HandianData.strokes_token_cache[(char, max_stroke_num)]

    wubi_token_cache:Dict[str, str] = dict()    
    @staticmethod
    def get_wubi_seq_cached(char:str):
        if HandianData.cursor is None:
            HandianData.init()
        if char not in HandianData.wubi_token_cache:
            if '\u4e00' <= char <= '\u9fff':
                query = f"select WuBi from Dicts where Char = '{char}'"
                HandianData.cursor.execute(query)
                rows = HandianData.cursor.fetchone()
                if rows[0] is not None:
                    seq = rows[0]
                    HandianData.wubi_token_cache[char] = seq
                else:
                    HandianData.zhengma_token_cache[char] = ''
            else:
                HandianData.wubi_token_cache[char] = ''
        return HandianData.wubi_token_cache[char]
    
    zhengma_token_cache:Dict[str, str] = dict()    
    @staticmethod
    def get_zhengma_seq_cached(char:str):
        if HandianData.cursor is None:
            HandianData.init()
        if char not in HandianData.zhengma_token_cache:
            if '\u4e00' <= char <= '\u9fff':
                query = f"select ZhengMa from Dicts where Char = '{char}'"
                HandianData.cursor.execute(query)
                rows = HandianData.cursor.fetchone()
                if rows[0] is not None:
                    seq = rows[0]
                    HandianData.zhengma_token_cache[char] = seq
                else:
                    HandianData.zhengma_token_cache[char] = ''
            else:
                HandianData.zhengma_token_cache[char] = ''
        return HandianData.zhengma_token_cache[char]